// packageI/pages/storageNew/storeageNew.js
const app = getApp()
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
var sourceType = [
  ['camera'],
  ['album'],
  ['camera', 'album']
]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: applet_park_api_url_imgUrl,
    token: wx.getStorageSync('TOKEN'),
    pics: [],
    imagepreList: [],
    imageList: [],
    name: '',
    brand: '',
    type: '',
    num: '',
    price: '',
    text: '',
    sources: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  upload() {
    var that = this;
    if (that.data.pics.length > 3) {
      wx.showToast({
        title: '最多上传3张图片',
        icon: "none"
      })
      return
    }
    wx.chooseImage({
      sourceType: sourceType[2],
      success: function (res) {
        var pics = that.data.pics.concat(res.tempFiles);
        var imageList = that.data.pics.concat(res.tempFiles);
        var imagepreList = that.data.imagepreList.concat(res.tempFilePaths);
        that.setData({
          pics: pics,
          imageList: imageList,
          imagepreList: imagepreList
        })
      }
    })
  },
  imgBigger(e) {
    wx.previewImage({
      current: e.currentTarget.dataset.src, //当前预览的图片
      urls: this.data.imagepreList, //所有要预览的图片
    })
  },
  changeName(e) {
    var that = this
    that.data.name = e.detail.value
    that.setData(that.data)
  },
  changeBrand(e) {
    var that = this
    that.data.brand = e.detail.value
    that.setData(that.data)
  },
  changeType(e) {
    var that = this
    that.data.type = e.detail.value
    that.setData(that.data)
  },
  changeNum(e) {
    var that = this
    that.data.num = e.detail.value
    that.setData(that.data)
  },
  changePrice(e) {
    var that = this
    that.data.price = e.detail.value
    that.setData(that.data)
  },
  changeText(e) {
    var that = this
    that.data.text = e.detail.value
    that.setData(that.data)
  },
  uploadImage() {
    var that = this
    if (that.data.name == 0 || that.data.name == '') {
      wx.showToast({
        title: '请输入器材名称',
      })
      return
    }
    if (that.data.brand == 0 || that.data.brand == '') {
      wx.showToast({
        title: '请输入品牌',
      })
      return
    }
    var that = this
    if (that.data.type == 0 || that.data.type == '') {
      wx.showToast({
        title: '请输入型号',
      })
      return
    }
    if (that.data.num == 0 || that.data.num == '') {
      wx.showToast({
        title: '请输入采购数量',
      })
      return
    }
    if (that.data.price == 0 || that.data.price == '') {
      wx.showToast({
        title: '请输入采购单价',
      })
      return
    }
    if (that.data.text == '') {
      wx.showToast({
        title: '请输入备注',
      })
      return
    }
    wx.showLoading({
      title: '图片上传中',
    })
    if (that.data.pics.length == 0) {
      wx.showToast({
        title: '请上传图片',
        icon: "none"
      })
      return
    }
    for (var i in that.data.pics) {
      wx.uploadFile({
        url: 'https://jinchengit.cn:9999/api/upload',
        filePath: that.data.pics[i].path,
        name: 'files',
        success(res) {
          let response = JSON.parse(res.data);
          that.data.imageList[i].picUrl = "http://zjurainforest.com:9999" + response.data[0];
          that.data.sources = that.data.sources + response.data + ",";
          that.setData(that.data);
          //全部传完了,则隐藏加载按钮并返回
          if (that.data.pics.length - 1 == i) { 
            wx.hideLoading();
            //数据回传录车页面,返回
            that.submit()
          }
        },
        fail(res) {
          wx.showToast({
            title: res.data,
            icon: "none"
          })
        }
      })
    }
  },
  submit() {
    var that = this
    wx.request({
      url: applet_park_api_url + '/api/consumableBillDetail',
      method: 'POST',
      header: {
        Authorization: that.data.token
      },
      data: {
        name: that.data.name,
        brand: that.data.brand,
        type: that.data.type,
        billType: 0,
        price: that.data.price,
        quantity: that.data.num,
        remark: that.data.text,
        images: that.data.sources
      },
      success: function (res) {
        if (res.data.code == 0) {
          wx.navigateBack();
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})